﻿using System;
using System.Drawing;
using System.Text;
using Neonode.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;

namespace StopWatch
{
    class Utilities
    {
        public static System.Drawing.Image background = Properties.Resources.bgWhite;
        
        [DllImport("coredll.dll")]
        private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);

        public static void KeepAwake(byte keyCode)
        {
            keybd_event(keyCode, 0x45, 1, (UIntPtr)0);
            keybd_event(keyCode, 0x45, 3, (UIntPtr)0);
        }

        public static void DrawAlphaImage(Assembly assembly, Graphics g, int x, int y, int width, int height, string resourceName)
        {
            IImage image;
            IImagingFactory factory = (IImagingFactory)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("327ABDA8-072B-11D3-9D7B-0000F81EF32E")));
            MemoryStream manifestResourceStream = (MemoryStream)assembly.GetManifestResourceStream(resourceName);
            factory.CreateImageFromBuffer(manifestResourceStream.GetBuffer(), (uint)manifestResourceStream.Length, BufferDisposalFlag.BufferDisposalFlagNone, out image);
            manifestResourceStream.Close();
            IntPtr hdc = g.GetHdc();
            Rectangle dstRect = new Rectangle(x, y, width + x, height + y);
            image.Draw(hdc, ref dstRect, IntPtr.Zero);
            g.ReleaseHdc(hdc);
        }

        public static void CheckBgColor()
        {
            RegistryKey key = Registry.LocalMachine.CreateSubKey(@"Software\Herbie\StopWatch\");

            switch ((int)key.GetValue("LCDColor"))
            {
                case 0:
                    background = Properties.Resources.bgWhite;
                    break;

                case 1:
                    background = Properties.Resources.bgRed;
                    break;

                case 2:
                    background = Properties.Resources.bgGreen;
                    break;

                case 3:
                    background = Properties.Resources.bgBlue;
                    break;

                default:
                    background = Properties.Resources.bgWhite;
                    break;
            }
            key.Close();
        }
    }

    [ComImport, Guid("327ABDA9-072B-11D3-9D7B-0000F81EF32E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), ComVisible(true)]
    public interface IImage
    {
        uint GetPhysicalDimension(out Size size);
        uint GetImageInfo(out ImageInfo info);
        uint SetImageFlags(uint flags);
        uint Draw(IntPtr hdc, ref Rectangle dstRect, IntPtr NULL);
        uint PushIntoSink();
        uint GetThumbnail(uint thumbWidth, uint thumbHeight, out IImage thumbImage);
    }

    [ComImport, ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("327ABDA7-072B-11D3-9D7B-0000F81EF32E")]
    public interface IImagingFactory
    {
        uint CreateImageFromStream();
        uint CreateImageFromFile(string filename, out IImage image);
        uint CreateImageFromBuffer([MarshalAs(UnmanagedType.LPArray)] byte[] buffer, uint size, BufferDisposalFlag disposalFlag, out IImage image);
        uint CreateNewBitmap();
        uint CreateBitmapFromImage();
        uint CreateBitmapFromBuffer();
        uint CreateImageDecoder();
        uint CreateImageEncoderToStream();
        uint CreateImageEncoderToFile();
        uint GetInstalledDecoders();
        uint GetInstalledEncoders();
        uint InstallImageCodec();
        uint UninstallImageCodec();
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct ImageInfo
    {
        public uint GuidPart1;
        public uint GuidPart2;
        public uint GuidPart3;
        public uint GuidPart4;
        public PixelFormatID pixelFormat;
        public uint Width;
        public uint Height;
        public uint TileWidth;
        public uint TileHeight;
        public double Xdpi;
        public double Ydpi;
        public uint Flags;
    }

    public enum BufferDisposalFlag
    {
        BufferDisposalFlagNone,
        BufferDisposalFlagGlobalFree,
        BufferDisposalFlagCoTaskMemFree,
        BufferDisposalFlagUnmapView
    }

    public enum PixelFormatID
    {
        PixelFormat16bppARGB1555 = 0x61007,
        PixelFormat16bppRGB555 = 0x21005,
        PixelFormat16bppRGB565 = 0x21006,
        PixelFormat1bppIndexed = 0x30101,
        PixelFormat24bppRGB = 0x21808,
        PixelFormat32bppARGB = 0x26200a,
        PixelFormat32bppPARGB = 0xe200b,
        PixelFormat32bppRGB = 0x22009,
        PixelFormat48bppRGB = 0x10300c,
        PixelFormat4bppIndexed = 0x30402,
        PixelFormat64bppARGB = 0x34400d,
        PixelFormat64bppPARGB = 0x1c400e,
        PixelFormat8bppIndexed = 0x30803,
        PixelFormatAlpha = 0x40000,
        PixelFormatCanonical = 0x200000,
        PixelFormatDontCare = 0,
        PixelFormatExtended = 0x100000,
        PixelFormatGDI = 0x20000,
        PixelFormatIndexed = 0x10000,
        PixelFormatMax = 15,
        PixelFormatPAlpha = 0x80000,
        PixelFormatUndefined = 0
    }
}
